package com.mall.manager.labellibrary.service;

import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * @功能 			: 	处理标签库表的增、删、改、查操作
 * @作者 			: 	WANGQI
 * @时间 			: 	2016.07.22
 * @所属公司 		: 	艾的教育>技术部
 * @copyright 	: 	Copyright www.ide365.com. All rights reserved
 */
@Service
public class ContentformLabelLibraryService {
	private Logger logger = LoggerFactory.getLogger(ContentformLabelLibraryService.class);
	@Resource
	private JdbcTemplate jdbcTemplate;

	/**
	 * @功能 		: 	添加标签库表信息
	 * @param 	: 	paraMap
	 * @return 	:
	 */
	public int contentform_labellibrary_add(Map<String, Object> paraMap) {
		String l_name = MapUtils.getString(paraMap, "l_name");
		int l_level = MapUtils.getIntValue(paraMap, "l_level");
		try {
			String sql = "INSERT INTO db_archive.t_label_library(l_name,l_level,l_create_date,l_update_date) VALUES (?, ?, NOW(), NOW())";
			int addNum = this.jdbcTemplate.update(sql, l_name, l_level);
			if (addNum > 0) {
				logger.info("标签信息添加成功！");
			}
			return addNum;
		} catch (Exception e) {
			logger.info("标签信息添加失败！【错误】:contentform_labellibrary_add方法中！");
			return 0;
		}
	}

	/**
	 * @功能 		: 	删除标签信息
	 * @param 	: 	paraMap
	 * @return 	:
	 */
	public int contentform_labellibrary_delete(Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "l_id");
		try {
			String sql = "DELETE from db_archive.t_label_library where l_id = ?";
			int deleteNum = jdbcTemplate.update(sql, id);
			if (deleteNum > 0) {
				logger.info("删除标签信息成功！");
			}
			return deleteNum;
		} catch (Exception e) {
			logger.info("删除标签信息失败！【错误】:contentform_labellibrary_delete方法中！");
			return 0;
		}
	}

	/**
	 * @功能 		: 	修改标签信息
	 * @param 	: 	paraMap
	 * @return 	:
	 */
	public int contentform_labellibrary_update(Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "l_id");
		String l_name = MapUtils.getString(paraMap, "l_name");
		String l_level = MapUtils.getString(paraMap, "l_level");
		String sql = "update db_archive.t_label_library set l_name=?,l_level=?,l_create_date=NOW(),l_update_date=NOW() where l_id=?";
		try {
			int updateNum = jdbcTemplate.update(sql, id, l_name, l_level);
			if (updateNum > 0) {
				logger.info("修改标签信息成功！");
			}
			return updateNum;
		} catch (Exception e) {
			logger.info("修改标签信息失败！【错误】:contentform_labellibrary_update方法中！");
			return 0;
		}
	}

	/**
	 * @功能 		: 	根据ID查询标签信息
	 * @param 	: 	paraMap
	 * @return 	:
	 */
	public Map<String, Object> contentform_labellibrary_findbyid(
			Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "l_id");
		try {
			String sql = "select l_id,l_name,l_level,l_create_date,l_update_date from db_archive.t_label_library where l_id = ?";
			Map<String, Object> partMap = jdbcTemplate.queryForMap(sql, id);
			if (partMap.size() > 0) {
				logger.info("成功查询到" + partMap.size() + "条数据！");
			}
			return partMap;
		} catch (Exception e) {
			logger.info("查询标签信息失败！【错误】:contentform_labellibrary_findbyid方法中！");
			return null;
		}
	}

}
